﻿using CityDashboard.Common;
using CityDashboard.Data;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Animation;
using Windows.UI.Xaml.Navigation;
using Windows.UI.Popups;
using System.Net;
using Telerik.UI.Xaml.Controls.Chart;
using Telerik.Charting;
using System.Threading;
using Windows.UI;
using Windows.UI.Xaml.Media.Imaging;

using System.Threading.Tasks;
using System.Collections.ObjectModel;
using Newtonsoft.Json;
using Windows.Storage;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
// The Grouped Items Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234231

namespace CityDashboard
{
    /// <summary>
    /// A page that displays a grouped collection of items.
    /// </summary>
    public sealed partial class GroupedItemsPage : Page
    {
        private NavigationHelper navigationHelper;
        private ObservableDictionary defaultViewModel = new ObservableDictionary();
        MainViewModel mainModel;
        private DispatcherTimer _timer = null;
        List<StoredLocalDataObj> storedData;
 
        /// <summary>
        /// NavigationHelper is used on each page to aid in navigation and 
        /// process lifetime management
        /// </summary>
        public NavigationHelper NavigationHelper
        {
            get { return this.navigationHelper; }
        }

        /// <summary>
        /// This can be changed to a strongly typed view model.
        /// </summary>
        public ObservableDictionary DefaultViewModel
        {
            get { return this.defaultViewModel; }
        }

        public GroupedItemsPage()
        {
            this.InitializeComponent();
            this.navigationHelper = new NavigationHelper(this);
            this.navigationHelper.LoadState += navigationHelper_LoadState;
     
        }


        /// <summary>
        /// Populates the page with content passed during navigation.  Any saved state is also
        /// provided when recreating a page from a prior session.
        /// </summary>
        /// <param name="sender">
        /// The source of the event; typically <see cref="NavigationHelper"/>
        /// </param>
        /// <param name="e">Event data that provides both the navigation parameter passed to
        /// <see cref="Frame.Navigate(Type, Object)"/> when this page was initially requested and
        /// a dictionary of state preserved by this page during an earlier
        /// session.  The state will be null the first time a page is visited.</param>
        private async void navigationHelper_LoadState(object sender, LoadStateEventArgs e)
        {
            // TODO: Create an appropriate data model for your problem domain to replace the sample data
            ProggressBarVisible(true);
            try
            {
                //var sampleDataGroups = await SampleDataSource.GetGroupsAsync();
                //this.DefaultViewModel["Groups"] = sampleDataGroups;
                this.DataContext = mainModel;
             
            }
            catch(Exception ex){
                ProggressBarVisible(false);
                messageBox("No connection !");
            }
            finally
            {
                ProggressBarVisible(false);
            }

           
           
        }



        /// <summary>
        /// Invoked when a group header is clicked.
        /// </summary>
        /// <param name="sender">The Button used as a group header for the selected group.</param>
        /// <param name="e">Event data that describes how the click was initiated.</param>
        void Header_Click(object sender, RoutedEventArgs e)
        {
            // Determine what group the Button instance represents
            var group = (sender as FrameworkElement).DataContext;

            // Navigate to the appropriate destination page, configuring the new page
            // by passing required information as a navigation parameter
            this.Frame.Navigate(typeof(GroupDetailPage), ((SampleDataGroup)group).UniqueId);
        }

        /// <summary>
        /// Invoked when an item within a group is clicked.
        /// </summary>
        /// <param name="sender">The GridView (or ListView when the application is snapped)
        /// displaying the item clicked.</param>
        /// <param name="e">Event data that describes the item clicked.</param>
        void ItemView_ItemClick(object sender, ItemClickEventArgs e)
        {
            // Navigate to the appropriate destination page, configuring the new page
            // by passing required information as a navigation parameter
           // var itemId = ((SampleDataItem)e.ClickedItem).UniqueId;
           // this.Frame.Navigate(typeof(ItemDetailPage), itemId);

            /*
            if (((SampleDataItem)e.ClickedItem).UniqueId == "Group-1-Item-1")
            {
                this.Frame.Navigate(typeof(ItemDetailPage), itemId);
            }
            else
            {
                this.Frame.Navigate(typeof(ItemDetailPage1), itemId);
            }
            */
        }


        #region NavigationHelper registration

        /// The methods provided in this section are simply used to allow
        /// NavigationHelper to respond to the page's navigation methods.
        /// 
        /// Page specific logic should be placed in event handlers for the  
        /// <see cref="GridCS.Common.NavigationHelper.LoadState"/>
        /// and <see cref="GridCS.Common.NavigationHelper.SaveState"/>.
        /// The navigation parameter is available in the LoadState method 
        /// in addition to page state preserved during an earlier session.

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            navigationHelper.OnNavigatedTo(e);
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            navigationHelper.OnNavigatedFrom(e);
        }

      
       
        #endregion

        private async void mainPage_Loaded(object sender, RoutedEventArgs e)
        {
            mainModel = new MainViewModel();
            this.DataContext = mainModel;

            if (_timer != null)
                _timer.Stop();
            // we always create a new instance of DispatcherTimer
            _timer = new DispatcherTimer();
            // if one second passes, that means our user has stopped interacting with the slider
            // we do real event
            _timer.Interval = new TimeSpan(1, 0, 0);
            _timer.Tick += RefreshPage;
            _timer.Start();

            //check StoredData
         
            try
            {
               await deserializeJsonAsync();
               ReLoadMainPage(false);
            }
            catch (Exception ex)
            {
                messageBox(ex.ToString());
            }
          
        }

        public void ReLoadMainPage(bool flag)
        {

            int temp_tonghs =flag? mainModel.SoHoso: storedData[0].HSNhan;
            int temp_tonghsdagq = flag? mainModel.SohsGQ: storedData[0].HSNhanDqg;
            int temp_tonghsDangGQ = flag? mainModel.SohsChuaGQ: storedData[0].HSNhanDangqg;
            int temp_SoKHDTTrinhUB = flag? mainModel.TrinhUBND: storedData[0].HSKHDTTrinh;
            int temp_SoKHDTChuaTrinhUB = flag? mainModel.HSChuaTrinhUBND: storedData[0].HSKHDTChuaTrinh;
            int temp_TrinhUBDaGiaiQuyet = flag? mainModel.TrinhUBND_gq: storedData[0].HSUBNDDaGQ;
            int temp_TrinhUBChuaGiaiQuyet = flag ? mainModel.TrinhUBND_cgq : storedData[0].HSUBNDDangGQ;
           
            if (flag == true)
            {
                mainModel.RefreshData();
            }
           

            // check data
            if (mainModel.SoHoso != temp_tonghs)
            {
                this.tonghs_tang.Text = "(" + Math.Abs(mainModel.SoHoso - temp_tonghs).ToString() + ")";
                if (mainModel.SoHoso > temp_tonghs)
                {
                    this.TongHS_Img.Source = new BitmapImage(new Uri("ms-appx:/Assets/won.png"));

                }
                else if (mainModel.SoHoso < temp_tonghs)
                {
                    this.TongHS_Img.Source = new BitmapImage(new Uri("ms-appx:/Assets/lost.png"));

                }
                this.tonghs_tang.Visibility = Windows.UI.Xaml.Visibility.Visible;
                this.TongHS_Img.Visibility = Windows.UI.Xaml.Visibility.Visible;
            }
            else
            {
                this.tonghs_tang.Text = "(0)";
            }

            if (mainModel.SohsGQ != temp_tonghsdagq)
            {
                this.TonghsDaGQ_tang.Text = "(" + Math.Abs(mainModel.SohsGQ - temp_tonghsdagq).ToString() + ")";
                if (mainModel.SohsGQ > temp_tonghsdagq)
                {
                    this.TonghsDaGQ_Img.Source = new BitmapImage(new Uri("ms-appx:/Assets/won.png"));
                }
                else if (mainModel.SohsGQ < temp_tonghsdagq)
                {
                    this.TonghsDaGQ_Img.Source = new BitmapImage(new Uri("ms-appx:/Assets/lost.png"));
                }
                this.TonghsDaGQ_tang.Visibility = Windows.UI.Xaml.Visibility.Visible;
                this.TonghsDaGQ_Img.Visibility = Windows.UI.Xaml.Visibility.Visible;
            }
            else
            {
                this.TonghsDaGQ_tang.Text = "(0)";
            }

            if (mainModel.SohsChuaGQ != temp_tonghsDangGQ)
            {
                this.TonghsDangGQ_tang.Text = "(" + Math.Abs(mainModel.SohsChuaGQ - temp_tonghsDangGQ).ToString() + ")";
                if (mainModel.SohsChuaGQ > temp_tonghsDangGQ)
                {
                    this.TonghsDangGQ_Img.Source = new BitmapImage(new Uri("ms-appx:/Assets/won.png"));
                }
                else if (mainModel.SohsChuaGQ < temp_tonghsDangGQ)
                {
                    this.TonghsDangGQ_Img.Source = new BitmapImage(new Uri("ms-appx:/Assets/lost.png"));
                }
                this.TonghsDangGQ_Img.Visibility = Windows.UI.Xaml.Visibility.Visible;
                this.TonghsDangGQ_tang.Visibility = Windows.UI.Xaml.Visibility.Visible;
            }
            else
            {
                this.TonghsDangGQ_tang.Text = "(0)";
            }

            if (mainModel.TrinhUBND != temp_SoKHDTTrinhUB)
            {
                this.SoKHDTTrinhUB_tang.Text = "(" + Math.Abs(mainModel.TrinhUBND - temp_SoKHDTTrinhUB).ToString() + ")";
                if (mainModel.TrinhUBND > temp_SoKHDTTrinhUB)
                {
                    this.SoKHDTTrinhUB_Img.Source = new BitmapImage(new Uri("ms-appx:/Assets/won.png"));
                }
                else if (mainModel.TrinhUBND < temp_SoKHDTTrinhUB)
                {
                    this.SoKHDTTrinhUB_Img.Source = new BitmapImage(new Uri("ms-appx:/Assets/lost.png"));
                }
                this.SoKHDTTrinhUB_tang.Visibility = Windows.UI.Xaml.Visibility.Visible;
                this.SoKHDTTrinhUB_Img.Visibility = Windows.UI.Xaml.Visibility.Visible;
            }
            else
            {
                this.SoKHDTTrinhUB_tang.Text = "(0)";
            }

            if (mainModel.HSChuaTrinhUBND != temp_SoKHDTChuaTrinhUB)
            {
                this.SoKHDTChuaTrinhUB_tang.Text = "(" + Math.Abs(mainModel.HSChuaTrinhUBND - temp_SoKHDTChuaTrinhUB).ToString() + ")";
                if (mainModel.HSChuaTrinhUBND > temp_SoKHDTChuaTrinhUB)
                {
                    this.SoKHDTChuaTrinhUB_Img.Source = new BitmapImage(new Uri("ms-appx:/Assets/won.png"));
                }
                else if (mainModel.HSChuaTrinhUBND < temp_SoKHDTChuaTrinhUB)
                {
                    this.SoKHDTChuaTrinhUB_Img.Source = new BitmapImage(new Uri("ms-appx:/Assets/lost.png"));
                }
                this.SoKHDTChuaTrinhUB_tang.Visibility = Windows.UI.Xaml.Visibility.Visible;
                this.SoKHDTChuaTrinhUB_Img.Visibility = Windows.UI.Xaml.Visibility.Visible;
            }
            else
            {
                this.SoKHDTChuaTrinhUB_tang.Text = "(0)";
            }

            if (mainModel.TrinhUBND_gq != temp_TrinhUBDaGiaiQuyet)
            {
                this.TrinhUBDaGiaiQuyet_tang.Text = "(" + Math.Abs(mainModel.TrinhUBND_gq - temp_TrinhUBDaGiaiQuyet).ToString() + ")";
                if (mainModel.TrinhUBND_gq > temp_TrinhUBDaGiaiQuyet)
                {
                    this.TrinhUBDaGiaiQuyet_Img.Source = new BitmapImage(new Uri("ms-appx:/Assets/won.png"));
                }
                else if (mainModel.TrinhUBND_gq < temp_TrinhUBDaGiaiQuyet)
                {
                    this.TrinhUBDaGiaiQuyet_Img.Source = new BitmapImage(new Uri("ms-appx:/Assets/lost.png"));
                }
                this.TrinhUBDaGiaiQuyet_tang.Visibility = Windows.UI.Xaml.Visibility.Visible;
                this.TrinhUBDaGiaiQuyet_Img.Visibility = Windows.UI.Xaml.Visibility.Visible;
            }
            else
            {
                this.TrinhUBDaGiaiQuyet_tang.Text = "(0)";
            }

            if (mainModel.TrinhUBND_cgq != temp_TrinhUBChuaGiaiQuyet)
            {
                this.TrinhUBChuaGiaiQuyet_tang.Text = "(" + Math.Abs(mainModel.TrinhUBND_cgq - temp_TrinhUBChuaGiaiQuyet).ToString() + ")";

                if (mainModel.TrinhUBND_cgq > temp_TrinhUBChuaGiaiQuyet)
                {
                    this.TrinhUBChuaGiaiQuyet_Img.Source = new BitmapImage(new Uri("ms-appx:/Assets/won.png"));
                }
                else if (mainModel.TrinhUBND_cgq < temp_TrinhUBChuaGiaiQuyet)
                {
                    this.TrinhUBChuaGiaiQuyet_Img.Source = new BitmapImage(new Uri("ms-appx:/Assets/lost.png"));
                }
                this.TrinhUBChuaGiaiQuyet_tang.Visibility = Windows.UI.Xaml.Visibility.Visible;
                this.TrinhUBChuaGiaiQuyet_Img.Visibility = Windows.UI.Xaml.Visibility.Visible;
            }
            else
            {
                this.TrinhUBChuaGiaiQuyet_tang.Text = "(0)";
            }


            //int temp_tonghs = mainModel.to
            // Don't forget to stop the timer :-)

            //  _timer.Stop();
            // _timer.Tick -= RefreshPage;

            ReLoadPageData();
        }

        async void RefreshPage(object sender, object e)
        {
           
            ReLoadMainPage(true);
            try
            {
                await writeJsonAsync();

            }
            catch (Exception ex)
            {
                messageBox(ex.ToString());
            }
           
        }

        public void ReLoadPageData()
        {
            this.tonghs.Text = mainModel.SoHoso.ToString();
            this.TonghsDaGQ.Text = mainModel.SohsGQ.ToString();
            this.TonghsDangGQ.Text = mainModel.SohsChuaGQ.ToString();
            this.TonghsDaGQ_per.Text = mainModel.dagq_phantram.ToString();
            this.TonghsDangGQ_per.Text = mainModel.chuagq_phantram.ToString();

            this.SoKHDTTrinhUB.Text = mainModel.TrinhUBND.ToString();
            this.SoKHDTChuaTrinhUB.Text = mainModel.HSChuaTrinhUBND.ToString();
            this.SoKHDTTrinhUB_per.Text = mainModel.TrinhUB_phantram.ToString();
            this.SoKHDTChuaTrinhUB_per.Text = mainModel.ChuatrinhUB_phantram.ToString();

            this.TrinhUBDaGiaiQuyet.Text = mainModel.TrinhUBND_gq.ToString();
            this.TrinhUBChuaGiaiQuyet.Text = mainModel.TrinhUBND_cgq.ToString();
            this.TrinhUBDaGiaiQuyet_per.Text = mainModel.trinhUBdagq_phantram.ToString();
            this.TrinhUBChuaGiaiQuyet_per.Text = mainModel.trinhUBchuagq_phantram.ToString();
        }

        #region chart
      
      
        #endregion
        #region Animation

        #endregion

        #region orthers function
        protected async void messageBox(string msg)
        {
            var msgDlg = new Windows.UI.Popups.MessageDialog(msg);
            msgDlg.DefaultCommandIndex = 1;
            await msgDlg.ShowAsync();
        }


        private void ProggressBarVisible(bool visible)
        {
            ProgressRingLoad.Visibility = visible ? Visibility.Visible : Visibility.Collapsed;
            ProgressRingLoad.IsActive = visible;
        }

        public void ChangeColumnName()
        {
           
        }
     
  
        private void Tracking_selectItem_clicked(object sender, DevExpress.UI.Xaml.Grid.SelectedItemChangedEventArgs e)
        {
            
            string str;
            //str = RadDataGrid2.GetSelectedItemCellValue("Trangthai_hs").ToString() + RadDataGrid2.GetSelectedItemCellValue("Xuly").ToString();
            //this.Frame.Navigate(typeof(ItemTrackingDetailPage1), str);
        }

        private void Tracking_select_clicked(object sender, DevExpress.UI.Xaml.Grid.GridSelectionChangedEventArgs e)
        {
          
        }

        private void Top10QuocGia_Tapped(object sender, TappedRoutedEventArgs e)
        {
            string str = "2";
            this.Frame.Navigate(typeof(ItemTrackingDetailPage1), str);
        }

        private void Top10QuocGiaUSD_Tapped(object sender, TappedRoutedEventArgs e)
        {
            string str = "4";
            this.Frame.Navigate(typeof(ItemTrackingDetailPage1), str);
        }


        private void Top5Dautu_Tapped(object sender, TappedRoutedEventArgs e)
        {
            string str = "3";
            this.Frame.Navigate(typeof(ItemTrackingDetailPage1), str);
        }


        private void HsnhanDetail_Tapped(object sender, TappedRoutedEventArgs e)
        {
            string str = "222";
            this.Frame.Navigate(typeof(ItemTrackingDetailPage1), str);
        }

        private void HsDagiaiQuyet_Tapped(object sender, TappedRoutedEventArgs e)
        {
            string str = "122";
            this.Frame.Navigate(typeof(ItemTrackingDetailPage1), str);
        }

        private void HsDangGiaiQuyet_Tapped(object sender, TappedRoutedEventArgs e)
        {
            string str = "022";
            this.Frame.Navigate(typeof(ItemTrackingDetailPage1), str);
        }

        private void HsTrinhDaGiaiQuyet_Tapped(object sender, TappedRoutedEventArgs e)
        {
           
            string str = "112";
            this.Frame.Navigate(typeof(ItemTrackingDetailPage1), str);
            
        }

        private void HsTrinhDangGiaiQuyet_Tapped(object sender, TappedRoutedEventArgs e)
        {
            string str = "012";
            this.Frame.Navigate(typeof(ItemTrackingDetailPage1), str);
        }

        private void HsTrinhUB_Tapped(object sender, TappedRoutedEventArgs e)
        {
            string str = "212";
            this.Frame.Navigate(typeof(ItemTrackingDetailPage1), str);
        }

        private void HsChuaTrinhUB_Tapped(object sender, TappedRoutedEventArgs e)
        {
            string str = "202";
            this.Frame.Navigate(typeof(ItemTrackingDetailPage1), str);
        }

        private void TrinhUBND_DangGiaiQuyet_Tre_Tapped(object sender, TappedRoutedEventArgs e)
        {
            string str = "010";
            this.Frame.Navigate(typeof(ItemTrackingDetailPage1), str);
        }

        private void TrinhUBND_Tre_Tapped(object sender, TappedRoutedEventArgs e)
        {
            string str = "210";
            this.Frame.Navigate(typeof(ItemTrackingDetailPage1), str);
        }

        private void KhongTrinhUBND_Tre_Tapped(object sender, TappedRoutedEventArgs e)
        {
            string str = "200";
            this.Frame.Navigate(typeof(ItemTrackingDetailPage1), str);
        }

        private void RadDataGrid2_ReCreateColumn(object sender, DevExpress.UI.Xaml.Grid.AutoGeneratingColumnEventArgs e)
        {
            if (e.Column.FieldName == "Trangthai_hs") { 
                e.Column.Header = "Trạng Thái";
                e.Column.Width = 180;
            }
            if (e.Column.FieldName == "Xuly")
            {
                e.Column.Header = "Tình Trạng";
                e.Column.Width = 150;
            }
            if (e.Column.FieldName == "Sl")
            {
                e.Column.Header = "Tổng";
                e.Column.Width = 100;
            }
           
          
        }

        private void RadDataGrid2_ReCreateColumn2(object sender, EventArgs e)
        {
           
        }

        private void DoubleTappedGrid(object sender, DoubleTappedRoutedEventArgs e)
        {
            string str;
            str = RadDataGrid2.GetSelectedItemCellValue("Trangthai_hs").ToString() + RadDataGrid2.GetSelectedItemCellValue("Xuly").ToString();
            this.Frame.Navigate(typeof(ItemTrackingDetailPage1), str);
        }

        private void HSNhanPointerMoved(object sender, PointerRoutedEventArgs e)
        {
           // this.tonghs.Foreground = new SolidColorBrush(Color.FromArgb(100, 153, 0, 0));
            this.tonghs.FontWeight = Windows.UI.Text.FontWeights.Bold;
        }

        private void HSNhanGotFocus(object sender, RoutedEventArgs e)
        {
            
        }

        private void HSNhanDragOver(object sender, DragEventArgs e)
        {
           
        }

        private void HSNhanPointerReleased(object sender, PointerRoutedEventArgs e)
        {
             
        }

        private void GridHSNPointerMoved(object sender, PointerRoutedEventArgs e)
        {
           // this.tonghs.FontWeight = Windows.UI.Text.FontWeights.Normal;
        }

   

        private void ChartUBNDTP_Tapped(object sender, TappedRoutedEventArgs e)
        {
            if (ubnd_chart.SelectedItem != null)
            {
                ObjectBase chart_UBND_Selected = (ObjectBase)ubnd_chart.SelectedItem;
                string str;
                if (chart_UBND_Selected.Flag == 1)
                {
                    switch (chart_UBND_Selected.Name)
                    {
                        case "Đúng hạn":
                                     str = "111";
                                    this.Frame.Navigate(typeof(ItemTrackingDetailPage1), str);
                            break;
                        case "Trễ hạn":
                                     str = "110";
                                    this.Frame.Navigate(typeof(ItemTrackingDetailPage1), str);
                            break;
                    }
               
                }
                else
                {
                    switch (chart_UBND_Selected.Name)
                    {
                        case "Đúng hạn":
                            str = "011";
                            this.Frame.Navigate(typeof(ItemTrackingDetailPage1), str);
                            break;
                        case "Trễ hạn":
                            str = "010";
                            this.Frame.Navigate(typeof(ItemTrackingDetailPage1), str);
                            break;
                    }

                }
            }
        
        }

        public void SetPopupPosition(PointerRoutedEventArgs e)
        {
           
        }
        private void TrinhUBMoved(object sender, PointerRoutedEventArgs e)
        {
            
        }


        #endregion

        private void ChuaTrinhUBMoved(object sender, PointerRoutedEventArgs e)
        {
            
        }

        private void HsTrinhUBDangTre(object sender, PointerRoutedEventArgs e)
        {
            
        }

   
        private void RefreshBtn_Taaped(object sender, TappedRoutedEventArgs e)
        {
            ReLoadMainPage(true);
           
        }

        private void PropertyChangedSoKHDT(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            messageBox(e.PropertyName);
           
        }

        private void chartSKHDT_Tapped(object sender, TappedRoutedEventArgs e)
        {

            if (SoKHDT_chart.SelectedItem != null)
            {
                ObjectBase chart_KHDT_Selected = (ObjectBase)SoKHDT_chart.SelectedItem;
                string str;
                if (chart_KHDT_Selected.Flag == 1)
                {
                    switch (chart_KHDT_Selected.Name)
                    {
                        case "Đúng hạn":
                            str = "211";
                            this.Frame.Navigate(typeof(ItemTrackingDetailPage1), str);
                            break;
                        case "Trễ hạn":
                            str = "210";
                            this.Frame.Navigate(typeof(ItemTrackingDetailPage1), str);
                            break;
                    }

                }
                else
                {
                    switch (chart_KHDT_Selected.Name)
                    {
                        case "Đúng hạn":
                            str = "201";
                            this.Frame.Navigate(typeof(ItemTrackingDetailPage1), str);
                            break;
                        case "Trễ hạn":
                            str = "200";
                            this.Frame.Navigate(typeof(ItemTrackingDetailPage1), str);
                            break;
                    }

                }
            }
        }

        private void HsDangGiaiQuyetChart_Tapped(object sender, TappedRoutedEventArgs e)
        {
            if (chart_danggq.SelectedItem != null)
            {
                ObjectBase chart_dagq_Selected = (ObjectBase)chart_danggq.SelectedItem;
                string str;

                switch (chart_dagq_Selected.Name)
                {
                    case "Đúng hạn":
                        str = "021";
                        this.Frame.Navigate(typeof(ItemTrackingDetailPage1), str);
                        break;
                    case "Trễ hạn":
                        str = "020";
                        this.Frame.Navigate(typeof(ItemTrackingDetailPage1), str);
                        break;
                }


            }
        }

        private void HsDaGiaiQuyetChart_Tapped(object sender, TappedRoutedEventArgs e)
        {

            if (chart_dagq.SelectedItem != null)
            {
                ObjectBase chart_dagq_Selected = (ObjectBase)chart_dagq.SelectedItem;
                string str;
              
                    switch (chart_dagq_Selected.Name)
                    {
                        case "Đúng hạn":
                            str = "121";
                            this.Frame.Navigate(typeof(ItemTrackingDetailPage1), str);
                            break;
                        case "Trễ hạn":
                            str = "120";
                            this.Frame.Navigate(typeof(ItemTrackingDetailPage1), str);
                            break;
                    }

               
            }
        }

        private void HSNhanPointerExited(object sender, PointerRoutedEventArgs e)
        {
            this.tonghs.FontWeight = Windows.UI.Text.FontWeights.Normal;
        }

        private void HSDagiaiQuyetMoved(object sender, PointerRoutedEventArgs e)
        {
            this.TonghsDaGQ.FontWeight = Windows.UI.Text.FontWeights.Bold;
        }

        private void HSDagiaiQuyetExited(object sender, PointerRoutedEventArgs e)
        {
            this.TonghsDaGQ.FontWeight = Windows.UI.Text.FontWeights.Normal;
        }

        private void HSDangGiaiQuyetMoved(object sender, PointerRoutedEventArgs e)
        {
            this.TonghsDangGQ.FontWeight = Windows.UI.Text.FontWeights.Bold;
        }

        private void HSDangGiaiQuyetExited(object sender, PointerRoutedEventArgs e)
        {
            this.TonghsDangGQ.FontWeight = Windows.UI.Text.FontWeights.Normal;
        }

        private void TrinhUBDaGiaiQuyetExited(object sender, PointerRoutedEventArgs e)
        {
            this.TrinhUBDaGiaiQuyet.FontWeight = Windows.UI.Text.FontWeights.Normal;
        }

        private void TrinhUBDaGiaiQuyetMoved(object sender, PointerRoutedEventArgs e)
        {
            this.TrinhUBDaGiaiQuyet.FontWeight = Windows.UI.Text.FontWeights.Bold;
        }

        private void TrinhUBChuaGiaiQuyetMoved(object sender, PointerRoutedEventArgs e)
        {
            this.TrinhUBChuaGiaiQuyet.FontWeight = Windows.UI.Text.FontWeights.Bold;
            
        }

        private void TrinhUBChuaGiaiQuyetExited(object sender, PointerRoutedEventArgs e)
        {
            this.TrinhUBChuaGiaiQuyet.FontWeight = Windows.UI.Text.FontWeights.Normal;
        }

        private void TrinhUBChuaGiaiQuyet_treExited(object sender, PointerRoutedEventArgs e)
        {
            this.TrinhUBChuaGiaiQuyet_tre.FontWeight = Windows.UI.Text.FontWeights.Normal;
            
        }

        private void TrinhUBChuaGiaiQuyet_treMoved(object sender, PointerRoutedEventArgs e)
        {
            this.TrinhUBChuaGiaiQuyet_tre.FontWeight = Windows.UI.Text.FontWeights.Bold;
        }

        private void SoKHDTTrinhUBExited(object sender, PointerRoutedEventArgs e)
        {
            this.SoKHDTTrinhUB.FontWeight = Windows.UI.Text.FontWeights.Normal;
        }

        private void SoKHDTTrinhUBMoved(object sender, PointerRoutedEventArgs e)
        {
            this.SoKHDTTrinhUB.FontWeight = Windows.UI.Text.FontWeights.Bold;
        }

        private void SoKHDTTrinhUB_treExited(object sender, PointerRoutedEventArgs e)
        {
            this.SoKHDTTrinhUB_tre.FontWeight = Windows.UI.Text.FontWeights.Normal;
        }

        private void SoKHDTTrinhUB_treMoved(object sender, PointerRoutedEventArgs e)
        {
            this.SoKHDTTrinhUB_tre.FontWeight = Windows.UI.Text.FontWeights.Bold;
        }

        private void SoKHDTChuaTrinhUBExited(object sender, PointerRoutedEventArgs e)
        {
            this.SoKHDTChuaTrinhUB.FontWeight = Windows.UI.Text.FontWeights.Normal;
        }

        private void SoKHDTChuaTrinhUBMoved(object sender, PointerRoutedEventArgs e)
        {
            this.SoKHDTChuaTrinhUB.FontWeight = Windows.UI.Text.FontWeights.Bold;
        }

        private void SoKHDTChuaTrinhUB_treExited(object sender, PointerRoutedEventArgs e)
        {
            this.SoKHDTChuaTrinhUB_tre.FontWeight = Windows.UI.Text.FontWeights.Normal;
        }

        private void SoKHDTChuaTrinhUB_treMoved(object sender, PointerRoutedEventArgs e)
        {
            this.SoKHDTChuaTrinhUB_tre.FontWeight = Windows.UI.Text.FontWeights.Bold;
        }

        private async void PageUnloaded(object sender, RoutedEventArgs e)
        {
        }

        #region Read/Write XML Json File
        public List<StoredLocalDataObj> CreateSaveObject()
        {
            List<StoredLocalDataObj> _data = new List<StoredLocalDataObj>();
            _data.Add(new StoredLocalDataObj()
            {
                HSNhan =   mainModel.SoHoso,
                HSNhanDqg = mainModel.SohsGQ,
                HSNhanDangqg = mainModel.SohsChuaGQ,
                HSKHDTTrinh = mainModel.TrinhUBND,
                HSKHDTChuaTrinh = mainModel.HSChuaTrinhUBND,
                HSUBNDDaGQ = mainModel.TrinhUBND_gq,
                HSUBNDDangGQ = mainModel.TrinhUBND_cgq
            });
            return _data;
        }
        public async Task writeXMLAsync()
        {
         
            string XMLFILENAME = "data.xml";
            var serializer = new DataContractSerializer(typeof(List<StoredLocalDataObj>));

            using (var stream = await ApplicationData.Current.LocalFolder.OpenStreamForWriteAsync(
                              XMLFILENAME,
                              CreationCollisionOption.ReplaceExisting))
            {
                serializer.WriteObject(stream, CreateSaveObject());
            }


        }

        private async Task writeJsonAsync()
        {
            // Notice that the write is ALMOST identical ... except for the serializer.

            string JSONFILENAME = "data.json";
            var serializer = new DataContractJsonSerializer(typeof(List<StoredLocalDataObj>));
            using (var stream = await ApplicationData.Current.LocalFolder.OpenStreamForWriteAsync(
                          JSONFILENAME,
                          CreationCollisionOption.ReplaceExisting))
            {
                serializer.WriteObject(stream, CreateSaveObject());
            }
        }


        private async Task readXMLAsync()
        {
            string content = String.Empty;
            string XMLFILENAME = "data.xml";
            var myStream = await ApplicationData.Current.LocalFolder.OpenStreamForReadAsync(XMLFILENAME);
           
            using (StreamReader reader = new StreamReader(myStream))
            {
                content = await reader.ReadToEndAsync();
            }

        }

        private async Task deserializeJsonAsync()
        {
            string content = String.Empty;
            string JSONFILENAME = "data.json";
         
            var jsonSerializer = new DataContractJsonSerializer(typeof(List<StoredLocalDataObj>));

            var myStream = await ApplicationData.Current.LocalFolder.OpenStreamForReadAsync(JSONFILENAME);

            storedData = (List<StoredLocalDataObj>)jsonSerializer.ReadObject(myStream);

        }

        public void checkStoredData()
        {
            foreach (var data in storedData)
            {

            }
        }

        #endregion

    }
}